$(function() {
  var shopInspectionListArr = [];
  // 检查记录 页面 弹出
  $('.shopInspectionBtn').on('click', function(event) {
    event.preventDefault();
    $('.shopInspection-pop').addClass('is-visible3');
    $('#shopInspectionModal .shopInspectionSearchBox input').val('');
    $('#shopInspectionPage .pageJump .text').val('');
    var reportingTimeStart = moment().format('YYYY-MM') + '-01';
    var reportingTimeEnd = moment().format('YYYY-MM-DD');
    $('#shopInspectionModal .reportingTimeStart').val(reportingTimeStart);
    $('#shopInspectionModal .reportingTimeEnd').val(reportingTimeEnd);
    reportingTimeDatepickerFn(reportingTimeStart, reportingTimeEnd);
    $.ajax({
      url: '/fireBrigadePatrolRecord/getStreet',
      async:false,
      success: function(res) {
        console.log(res);
        if (res.status == 200) {
          $(
            '#shopInspectionModal .shopInspectionSearchBox .communitySelect'
          ).html('<option value="">全部</option>');
          for (var i = 0; i < res.data.length; i++) {
            $(
              '#shopInspectionModal .shopInspectionSearchBox .communitySelect'
            ).append(
              '<option value="' +
                res.data[i].id +
                '">' +
                res.data[i].street +
                '</option>'
            );
          }
        }
      }
    });
    shopInspectionPageList(1);
  });

  // 检查记录 页面 关闭
  $('.shopInspection-pop').on('click', function(event) {
    if (
      $(event.target).is('.cd-popup-close') ||
      $(event.target).is('.shopInspection-pop')
    ) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });
  // 搜索
  $('#shopInspectionSearchBtn').click(function() {
    shopInspectionPageList(1);
  });

  function shopInspectionPageList(n) {
    var size = 10;
    var shopName = $(
      '#shopInspectionModal .shopInspectionSearchBox .shopName'
    ).val();
    var inspectedPerson = $(
      '#shopInspectionModal .shopInspectionSearchBox .inspectedPerson'
    ).val();
    var beginTime = $('#shopInspectionModal .reportingTimeStart').val();
    var endTime = $('#shopInspectionModal .reportingTimeEnd').val();
    var community = $('#shopInspectionModal .communitySelect').val();
    console.log(community)
    $.ajax({
      url:
        '/fireBrigadePatrolRecord/getList?beginTime=' +
        beginTime +
        '&endTime=' +
        endTime +
        '&shopName=' +
        shopName +
        '&inspectedPerson=' +
        inspectedPerson +
        '&street=' +
        community +
        '&size=' +
        size +
        '&page=' +
        (+n - 1),
      success: function(res) {
        console.log(res);
        if (res.status == 200) {
          $('#shopInspectionSelectAll').prop('checked', false);
          var content = res.data.content;
          shopInspectionListArr = content;
          var pageSum = res.data.totalPages;
          if (pageSum > 0) {
            $('#shopInspectionPageBox').show();
          }
          $('#shopInspectionModal .shopInspectionList .tbodyList').html('');
          for (var i = 0; i < content.length; i++) {
            $('#shopInspectionModal .shopInspectionList .tbodyList').append(
              '<tr><td><input type="checkbox" class="check"></td><td>' +
                ((+n - 1) * +10 + +i + +1) +
                '</td><td>' +
                content[i].shopName +
                '</td><td>' +
                content[i].shopAddress +
                '</td><td>' +
                content[i].time +
                '</td><td>' +
                content[i].inspectedPerson +
                '</td><td>' +
                content[i].telephone +
                '</td><td>' +
                content[i].street +
                '</td><td><span class="downLoad">下载</span></td></tr>'
            );
          }
          shopInspectionPageFunc(pageSum, n);
        }
      }
    });
  }

  function reportingTimeDatepickerFn(reportingTimeStart, reportingTimeEnd) {
    console.log(reportingTimeStart, reportingTimeEnd);
    $('#shopInspectionModal .reportingTimeStart').datepicker({
      language: 'zh-CN',
      autoclose: true,
      startView: 0,
      format: 'yyyy-mm-dd',
      showSecond: true, //显示秒
      timeFormat: 'HH:mm:ss', //格式化时间
      stepHour: 1, //设置步长
      stepMinute: 1,
      stepSecond: 1,
      clearBtn: false,
      todayBtn: false,
      viewDate: reportingTimeStart
    });

    $('#shopInspectionModal .reportingTimeEnd').datepicker({
      language: 'zh-CN',
      autoclose: true,
      startView: 0,
      format: 'yyyy-mm-dd',
      showSecond: true, //显示秒
      timeFormat: 'HH:mm:ss', //格式化时间
      stepHour: 1, //设置步长
      stepMinute: 1,
      stepSecond: 1,
      clearBtn: false,
      todayBtn: false,
      viewDate: reportingTimeEnd
    });
  }
  // 列表分页
  function shopInspectionPageFunc(pageSum, startPageNum) {
    console.log(pageSum);
    if (pageSum <= 1) {
      $('#shopInspectionPageBox .pageJump').hide();
    } else {
      $('#shopInspectionPageBox .pageJump').show();
    }
    Page({
      num: pageSum, //页码数
      startnum: startPageNum, //指定页码
      elem: $('#shopInspectionPage'), //指定的元素
      callback: function(n) {
        //回调函数
        console.log(n);
        shopInspectionPageList(n);
        // 当翻到下一页时 清除掉 全选按钮的 选中状态
        $('#shopInspectionSelectAll').prop('checked', false);
      }
    });
  }
  //全选删除/全不选
  $('#shopInspectionSelectAll').click(function() {
    var current = $(this).prop('checked');
    $('.check').prop({
      checked: current
    });
  });

  $('#shopInspectionModal').on('click', '.check', function() {
    var flag = true;
    //  遍历下面所有的状态
    for (var i = 0; i < $('.check').length; i++) {
      //        如果其中一个不满足条件，把状态设置成false，立刻跳出循环
      if (
        $('.check')
          .eq(i)
          .prop('checked') == false
      ) {
        flag = false;
        break;
      }
    }
    $('#shopInspectionSelectAll').prop({
      checked: flag
    });
  });
  function shopInspectionDownFn(index) {
    $('#shopInspectionDown .tbodyList .shopAddress').text(
      shopInspectionListArr[index].shopAddress
    );
    $('#shopInspectionDown .tbodyList .shopName').text(
      shopInspectionListArr[index].shopName
    );
    $('#shopInspectionDown .tbodyList .coveredArea').text(
      shopInspectionListArr[index].coveredArea
    );
    $('#shopInspectionDown .tbodyList .shopManager').text(
      shopInspectionListArr[index].shopManager
    );
    $('#shopInspectionDown .tbodyList .telephone').text(
      shopInspectionListArr[index].telephone
    );
    $('#shopInspectionDown .tbodyList .firSafetyOfficer').text(
      shopInspectionListArr[index].firSafetyOfficer
    );
    $('#shopInspectionDown .tbodyList .officerTelephone').text(
      shopInspectionListArr[index].officerTelephone
    );
    $('#shopInspectionDown .tbodyList .email').text(
      shopInspectionListArr[index].email
    );
    $('#shopInspectionDown .tbodyList .street').text(
      shopInspectionListArr[index].street
    );
    $('#shopInspectionDown .tbodyList .businessScope').text(
      shopInspectionListArr[index].businessScope
    );
    $('#shopInspectionDown table .time').text(
      shopInspectionListArr[index].time
    );
    var info1 = shopInspectionListArr[index].info1 == 1 ? '是' : '否';
    var info2 = shopInspectionListArr[index].info2 == 1 ? '是' : '否';
    var info3 = shopInspectionListArr[index].info3 == 1 ? '是' : '否';
    var info4 = shopInspectionListArr[index].info4 == 1 ? '是' : '否';
    var info5 = shopInspectionListArr[index].info5 == 1 ? '是' : '否';
    var info6 = shopInspectionListArr[index].info6 == 1 ? '是' : '否';
    var info7 = shopInspectionListArr[index].info7 == 1 ? '是' : '否';
    var info8 = shopInspectionListArr[index].info8 == 1 ? '是' : '否';
    var info9 = shopInspectionListArr[index].info9 == 1 ? '是' : '否';
    var info10 = shopInspectionListArr[index].info10 == 1 ? '是' : '否';
    $('#shopInspectionDown .tbodyList .info1').text(info1);
    $('#shopInspectionDown .tbodyList .info2').text(info2);
    $('#shopInspectionDown .tbodyList .info3').text(info3);
    $('#shopInspectionDown .tbodyList .info4').text(info4);
    $('#shopInspectionDown .tbodyList .info5').text(info5);
    $('#shopInspectionDown .tbodyList .info6').text(info6);
    $('#shopInspectionDown .tbodyList .info7').text(info7);
    $('#shopInspectionDown .tbodyList .info8').text(info8);
    $('#shopInspectionDown .tbodyList .info9').text(info9);
    $('#shopInspectionDown .tbodyList .info10').text(info10);
    $('#shopInspectionDown .tbodyList .remark').text(
      shopInspectionListArr[index].remark
    );
    $('#shopInspectionDown .tbodyList .inspectedPerson').text(
      shopInspectionListArr[index].inspectedPerson
    );
    $('#shopInspectionDown .serialNumber').text(
      shopInspectionListArr[index].serialNumber
    );
    $('#shopInspectionDown .tbodyList .namePhotoUrl').attr(
      'src',
      shopInspectionListArr[index].namePhotoUrl
    );
    $('#shopInspectionDown .tbodyList .street').text(
      shopInspectionListArr[index].street
    );
    $('#shopInspectionDown .tbodyList .street').text(
      shopInspectionListArr[index].street
    );
    // 下载Word
    $('#shopInspectionDown').wordExport(
      shopInspectionListArr[index].serialNumber
    );
    console.log(index, shopInspectionListArr[index]);
  }
  // 单个下载
  $('#shopInspectionModal').on('click', '.downLoad', function() {
    var index = $(this)
      .parent()
      .parent()
      .index();
    shopInspectionDownFn(index);
  });
  // 点击批量下载
  $('#shopInspectionModal .shopInspectionLoadBtn').on('click', function() {
    if ($('#shopInspectionSelectAll').prop('checked')) {
      console.log('全选');
    }
    $(
      '#shopInspectionModal .table .tbodyList input[type=checkbox]:checked'
    ).each(function() {
      console.log(
        $(this)
          .parent()
          .parent()
          .index()
      );
      var index = $(this)
        .parent()
        .parent()
        .index();
      shopInspectionDownFn(index);
    });
    $('#shopInspectionSelectAll').prop('checked', false);
    $(
      '#shopInspectionModal .table .tbodyList input[type=checkbox]:checked'
    ).prop('checked', false);
  });

  // 调试：检查记录打印 Word 页面
  // $('.shopInspection-down-pop').addClass('is-visible3');
  // // 批量下载
  // $.ajax({
  //   url: '/fireBrigadePatrolRecord/getList?beginTime=2019-01-11 11:09:59&endTime=2019-01-19 19:10:59&shopName=&inspectedPerson=&size=10&page=0',
  //   success: function (res) {
  //     console.log(res);
  //     var content = res.data.content;
  //     // for(var i=0;i<content.length;i++){
  //     $('#shopInspectionDown .tbodyList .shopAddress').text(content[2].shopAddress);
  //     $('#shopInspectionDown .tbodyList .shopName').text(content[2].shopName);
  //     $('#shopInspectionDown .tbodyList .coveredArea').text(content[2].coveredArea);
  //     $('#shopInspectionDown .tbodyList .shopManager').text(content[2].shopManager);
  //     $('#shopInspectionDown .tbodyList .telephone').text(content[2].telephone);
  //     $('#shopInspectionDown .tbodyList .firSafetyOfficer').text(content[2].firSafetyOfficer);
  //     $('#shopInspectionDown .tbodyList .officerTelephone').text(content[2].officerTelephone);
  //     $('#shopInspectionDown .tbodyList .email').text(content[2].email);
  //     $('#shopInspectionDown .tbodyList .street').text(content[2].street);
  //     var info1 = content[2].info1 == 1 ? '是' : '否';
  //     var info2 = content[2].info2 == 1 ? '是' : '否';
  //     var info3 = content[2].info3 == 1 ? '是' : '否';
  //     var info4 = content[2].info4 == 1 ? '是' : '否';
  //     var info5 = content[2].info5 == 1 ? '是' : '否';
  //     var info6 = content[2].info6 == 1 ? '是' : '否';
  //     var info7 = content[2].info7 == 1 ? '是' : '否';
  //     var info8 = content[2].info8 == 1 ? '是' : '否';
  //     var info9 = content[2].info9 == 1 ? '是' : '否';
  //     var info10 = content[2].info10 == 1 ? '是' : '否';
  //     $('#shopInspectionDown .tbodyList .info1').text(info1);
  //     $('#shopInspectionDown .tbodyList .info2').text(info2);
  //     $('#shopInspectionDown .tbodyList .info3').text(info3);
  //     $('#shopInspectionDown .tbodyList .info4').text(info4);
  //     $('#shopInspectionDown .tbodyList .info5').text(info5);
  //     $('#shopInspectionDown .tbodyList .info6').text(info6);
  //     $('#shopInspectionDown .tbodyList .info7').text(info7);
  //     $('#shopInspectionDown .tbodyList .info8').text(info8);
  //     $('#shopInspectionDown .tbodyList .info9').text(info9);
  //     $('#shopInspectionDown .tbodyList .info10').text(info10);
  //     $('#shopInspectionDown .tbodyList .remark').text(content[2].remark);
  //     $('#shopInspectionDown .tbodyList .inspectedPerson').text(content[2].inspectedPerson);
  //     $('#shopInspectionDown .serialNumber').text(content[2].serialNumber);
  //     $('#shopInspectionDown .tbodyList .namePhotoUrl').attr('src',content[2].namePhotoUrl);
  //     $('#shopInspectionDown .tbodyList .street').text(content[2].street);
  //     $('#shopInspectionDown .tbodyList .street').text(content[2].street);
  //     // 下载Word
  //     $("#shopInspectionDown").wordExport(content[2].serialNumber);
  //     // }
  //   }
  // });

  $('.downLoadTest').click(function(event) {
    $('#shopInspectionDown').wordExport();
  });

  // 点击全部下载 页面 弹出
  $('.shopInspectionDownBtn').on('click', function(event) {
    event.preventDefault();
    $('.shopInspection-down-pop').addClass('is-visible3');
  });
  // 点击全部下载 页面 关闭
  $('.shopInspection-down-pop').on('click', function(event) {
    if (
      $(event.target).is('.cd-popup-close') ||
      $(event.target).is('.shopInspection-down-pop')
    ) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });
});
